package com.fowo.api.service.handleWork;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.BdRate;
import com.fowo.api.mapper.BdRateMapper;
import com.fowo.api.model.bd.rate.BdRateSyncRatesAction;
import com.fowo.api.service.handleWork.rate.rateList;
import com.fowo.api.service.impl.BdRateServiceImpl;
import com.fowo.api.util.LocalDateUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.net.http.HttpResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class synCRate implements BdRateSyncRatesAction {

    @Resource
    private BdRateServiceImpl rateServiceImpl;
    private String url;
    @Resource
    private BdRateMapper bdRateMapper;

    public synchronized void getDate(){
        //【万维易源】银行汇率查询-汇率转换-外汇币种-历史汇率数据
        String endDate = LocalDateUtils.format(LocalDateUtils.lastDayOfMonth(new Date()), LocalDateUtils.DATE_PATTERN);
        String startDate = LocalDateUtils.format(LocalDateUtils.firstDayOfMonth(new Date()), LocalDateUtils.DATE_PATTERN);
        //获取上个月
        Date date = LocalDateUtils.addDateMonths(LocalDateUtils.lastDayOfMonth(new Date()), -1);
        String upEndDate = LocalDateUtils.format(LocalDateUtils.lastDayOfMonth(date), LocalDateUtils.DATE_PATTERN);
        String upStartDate = LocalDateUtils.format(LocalDateUtils.firstDayOfMonth(date), LocalDateUtils.DATE_PATTERN);


        String host = "https://ali-waihui.showapi.com";
        String path = "/waihui-list";
        String url= host+path;
        String appcode = "ced1664d6a5b4044aa062deedf55bd17";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "APPCODE " + appcode);
        HttpEntity<String> entity = new HttpEntity<>(headers);
        rateList rateList=null;
        try {
            RestTemplate REST_TEMPLATE = new RestTemplate();
            String response = REST_TEMPLATE.exchange(url, HttpMethod.GET, entity, String.class).getBody();
             rateList = JSON.parseObject(response, rateList.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(rateList.getShowapi_res_code()==0){
            com.fowo.api.service.handleWork.rate.rateList.ShowapiResBodyBean showapiResBody = rateList.getShowapi_res_body();
            for (int i=0;i<showapiResBody.getList().size();i++){
                com.fowo.api.service.handleWork.rate.rateList.ShowapiResBodyBean.ListBean listBean = showapiResBody.getList().get(i);
                List<BdRate> list = rateServiceImpl.list(Wrappers.<BdRate>query()
                        .lambda().between(BdRate::getDate, startDate+" 00:00:00",endDate+" 23:59:59")
                        .eq(BdRate::getName, listBean.getName()));
                BigDecimal multiply = new BigDecimal(listBean.getZhesuan()).divide(new BigDecimal("100"),10,BigDecimal.ROUND_HALF_UP);
                BdRate rate = new BdRate();
                rate.setDate(new Date());
                rate.setName(listBean.getName());
                rate.setOfficialRate(multiply);
//                rate.setMyRate(multiply);
                rate.setRateEg(listBean.getCode());
                rate.setTargetName("人民币");
                rate.setOriginalName(listBean.getName());
                if(list.size()==0){
                    //查上个月我的汇率
                    List<BdRate> list2 = rateServiceImpl.list(Wrappers.<BdRate>query()
                            .lambda().between(BdRate::getDate, upStartDate+" 00:00:00",upEndDate+" 23:59:59")
                            .eq(BdRate::getName, listBean.getName()));
                    if(list2.size()>0){
                        rate.setMyRate(list2.get(0).getMyRate());
                    }
                    rateServiceImpl.save(rate);
                }else{
                    BdRate bdRate = list.get(0);
                    rate.setId(bdRate.getId());
                    rateServiceImpl.updateById(rate);
                }
            }
        }
        bdRateMapper.upkeepBdRateDetail();
    }


    public synchronized void getDate2(){
        String endDate = LocalDateUtils.format(LocalDateUtils.lastDayOfMonth(new Date()), LocalDateUtils.DATE_PATTERN);
        String startDate = LocalDateUtils.format(LocalDateUtils.firstDayOfMonth(new Date()), LocalDateUtils.DATE_PATTERN);
        String host = "http://api.k780.com";
        String url= host;
        String AppKey="72512";
        String Sign="c078e9fef9ce5b061b6b2c7652674b7d";
        String gjson = "";
        HttpHeaders headers = new HttpHeaders();
//        headers.set("Authorization", "APPCODE " + appcode);

        HttpEntity<String> entity = new HttpEntity<>(headers);
        rateList rateList=null;
        try {
            RestTemplate REST_TEMPLATE = new RestTemplate();
            String response = REST_TEMPLATE.exchange(url, HttpMethod.GET, entity, String.class).getBody();
            rateList = JSON.parseObject(response, rateList.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(rateList.getShowapi_res_code()==0){
            com.fowo.api.service.handleWork.rate.rateList.ShowapiResBodyBean showapiResBody = rateList.getShowapi_res_body();
            for (int i=0;i<showapiResBody.getList().size();i++){
                com.fowo.api.service.handleWork.rate.rateList.ShowapiResBodyBean.ListBean listBean = showapiResBody.getList().get(i);
                List<BdRate> list = rateServiceImpl.list(Wrappers.<BdRate>query()
                        .lambda().between(BdRate::getDate, startDate+" 00:00:00",endDate+" 23:59:59")
                        .eq(BdRate::getName, listBean.getName()));
                BigDecimal multiply = new BigDecimal(listBean.getZhesuan()).divide(new BigDecimal("100"),10,BigDecimal.ROUND_HALF_UP);
                BdRate rate = new BdRate();
                rate.setDate(new Date());
                rate.setName(listBean.getName());
                rate.setOfficialRate(multiply);
                rate.setMyRate(multiply);
                rate.setRateEg(listBean.getCode());
                rate.setTargetName("人民币");
                rate.setOriginalName(listBean.getName());
                if(list.size()==0){
                    rateServiceImpl.save(rate);
                }else{
                    BdRate bdRate = list.get(0);
                    rate.setId(bdRate.getId());
                    rateServiceImpl.updateById(rate);
                }
            }
        }


    }

    @Override
    public void run() throws Exception {
        getDate();
    }
}
